package com.zlz.server.controller;

import com.zlz.server.annotation.OperateLog;
import com.zlz.server.enums.OprLevel;
import com.zlz.server.pojo.Admin;
import com.zlz.server.pojo.RespBean;
import com.zlz.server.service.IAdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: zlz
 * @date: 2021/3/21 14:18
 * @description:
 * @since: JDK1.8
 */
@RestController
public class AdminInfoController {
    @Resource
    private IAdminService iAdminService;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/admin/info")
    @OperateLog(moduleName = "操作员", operationName = "更新当前用户信息", level = OprLevel.NORMAL)
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication) {
        if (iAdminService.updateById(admin)) {
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,
                    null, authentication.getAuthorities()));
            return RespBean.success("更新成功!", null);
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "更新密码")
    @PutMapping("/admin/pwd")
    @OperateLog(moduleName = "操作员", operationName = "更新密码", level = OprLevel.NORMAL)
    public RespBean updateAdminPwd(@RequestBody Map<String, Object> info) {
        String oldPwd = (String)info.get("oldPwd");
        String pwd = (String)info.get("pwd");
        String adminId = (String)info.get("adminId");
        iAdminService.updateAdminPwd(oldPwd, pwd, adminId);
        return RespBean.success();
    }
}
